import { httpClient } from '@/utils/http/axios';
import { ErrorMessageMode } from '#/axios';
import { PageRequest, PageData } from '@/utils/common/types';
import { AjaxResult } from '@/api/model';
import { MessageInputDto, MessageOutputDto, MessageReceiverOutputDto } from './model';

/** 管理-消息URL枚举 */
enum Api {
  /** 读取消息 */
  Read = '/api/admin/message/read',
  /** 读取消息接收者 */
  ReadReceiver = '/api/admin/message/readReceiver',
  /** 新增消息 */
  Create = '/api/admin/message/create',
  /** 更新消息 */
  Update = '/api/admin/message/update',
  /** 删除消息 */
  Delete = '/api/admin/message/delete',
  /** 发送消息 */
  Send = '/api/admin/message/send',
}

/**
 * 读取消息
 * @param request 分页请求
 * @param mode 错误消息模型
 */
export function readMessages(request: PageRequest, mode: ErrorMessageMode = 'message') {
  return httpClient.post<PageData<MessageOutputDto>>(
    {
      url: Api.Read,
      params: request,
    },
    {
      errorMessageMode: mode,
    },
  );
}

/**
 * 读取消息接收者
 * @param request 分页请求
 * @param mode 错误消息模型
 */
export function readMessageReceivers(request: PageRequest, mode: ErrorMessageMode = 'message') {
  return httpClient.post<PageData<MessageReceiverOutputDto>>(
    {
      url: Api.ReadReceiver,
      params: request,
    },
    {
      errorMessageMode: mode,
    },
  );
}

/**
 * 新增消息
 * @param dtos 消息DTO
 * @param mode 错误消息模型
 */
export function createMessages(dtos: MessageInputDto[], mode: ErrorMessageMode = 'message') {
  return httpClient.post<AjaxResult>(
    {
      url: Api.Create,
      params: dtos,
    },
    {
      errorMessageMode: mode,
    },
  );
}

/**
 * 更新消息
 * @param dtos 消息DTO
 * @param mode 错误消息模型
 */
export function updateMessages(dtos: MessageInputDto[], mode: ErrorMessageMode = 'message') {
  return httpClient.post<AjaxResult>(
    {
      url: Api.Update,
      params: dtos,
    },
    {
      errorMessageMode: mode,
    },
  );
}

/**
 * 删除消息
 * @param ids 消息编号
 * @param mode 错误消息模型
 */
export function deleteMessages(ids: (string | bigint)[], mode: ErrorMessageMode = 'message') {
  return httpClient.post<AjaxResult>(
    {
      url: Api.Delete,
      params: ids,
    },
    {
      errorMessageMode: mode,
    },
  );
}

/**
 * 发送消息
 * @param ids 消息编号
 * @param mode 错误消息模型
 */
export function sendMessages(ids: (string | bigint)[], mode: ErrorMessageMode = 'message') {
  return httpClient.post<AjaxResult>(
    {
      url: Api.Send,
      params: ids,
    },
    {
      errorMessageMode: mode,
    },
  );
}
